<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="关键字" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="项目名称或编码搜索"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新状态" prop="updateStatus">
        <el-select
          v-model="queryParams.updateStatus"
          placeholder="请选择更新状态"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.update_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 添加导出按钮和统计信息 -->
    <div class="mb-8 disFlxAC">
      <el-button
        class="mr-10"
        type="info"
        plain
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
        >导出</el-button
      >
      <div class="statistics-info">
        总共价格变更 <span class="text-primary">{{ totalCount }}</span> 条数据,
        价格更新成功 <span class="text-success">{{ successCount }}</span> 条,
        失败 <span class="text-danger">{{ failCount }}</span> 条
      </div>
    </div>

    <el-table
      v-loading="loading"
      :data="historyList"
      border
      style="width: 100%"
    >
      <el-table-column label="项目名称" prop="itemName" align="center" />
      <el-table-column
        label="项目编码"
        prop="itemCode"
        align="center"
        width="120"
      />
      <el-table-column label="项目类别" prop="dictLabel" align="center" />
      <el-table-column label="项目售价" align="center" width="120">
        <template slot-scope="{ row }"> ￥{{ row.checkPrice }} </template>
      </el-table-column>
      <el-table-column label="更新后售价" align="center" width="120">
        <template slot-scope="{ row }"> ￥{{ row.checkPriceAfter }} </template>
      </el-table-column>
      <el-table-column label="项目采购价" align="center" width="120">
        <template slot-scope="{ row }"> ￥{{ row.purchasePrice }} </template>
      </el-table-column>
      <el-table-column label="更新后采购价" align="center" width="120">
        <template slot-scope="{ row }">
          ￥{{ row.purchasePriceAfter }}
        </template>
      </el-table-column>
      <el-table-column label="更新状态" align="center" width="100">
        <template slot-scope="{ row }">
          <dict-tag
            :options="dict.type.update_status"
            :value="String(row.updateStatus)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="备注说明"
        prop="remark"
        align="center"
        width="180"
      />
      <el-table-column
        label="更新时间"
        prop="updateTime"
        align="center"
        width="180"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getPriceHistory, getPriceHistoryStats } from "@/api/service";

export default {
  name: "PriceHistory",
  dicts: ['update_status'],
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      historyList: [], // 变更记录列表
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined, // 关键字搜索
        updateStatus: undefined // 更新状态
      },
      totalCount: 0, // 总数
      successCount: 0, // 成功数
      failCount: 0, // 失败数
    };
  },
  created() {
    this.getList();
    this.getStats(); // 获取统计数据
  },
  methods: {
    // 获取统计数据
    async getStats() {
      try {
        const response = await getPriceHistoryStats();
        if (response.code === 200 && response.data) {
          this.totalCount = response.data.totalNum;
          this.successCount = response.data.succNum;
          this.failCount = response.data.failNum;
        }
      } catch (error) {
        console.error('获取统计数据失败:', error);
      }
    },
    async getList() {
      try {
        this.loading = true;
        const response = await getPriceHistory(this.queryParams);
        this.historyList = response.rows;
        this.total = response.total;
      } catch (error) {
        console.error('获取价格变更记录失败:', error);
      } finally {
        this.loading = false;
      }
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getStats(); // 查询时更新统计数据
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport() {
      this.download(
        'check/item/update/record/export',
        {
          ...this.queryParams
        },
        `价格变更记录_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>

<style lang="scss" scoped>
.statistics-info {
  font-size: 16px;
  .text-primary {
    color: #409eff;
    font-weight: bold;
    margin: 0 4px;
  }

  .text-success {
    color: #67c23a;
    font-weight: bold;
    margin: 0 4px;
  }

  .text-danger {
    color: #f56c6c;
    font-weight: bold;
    margin: 0 4px;
  }
}
</style>
